package com.ruoyi.app.controller.product.service.impl;

import com.ruoyi.app.controller.product.mapper.IProductCategoryMapper;
import com.ruoyi.app.controller.product.mapper.IProductMapper;
import com.ruoyi.app.controller.product.service.IProductCategoryService;
import com.ruoyi.app.controller.product.service.IProductService;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.entity.VO.product.PetrolProductVO;
import com.ruoyi.common.entity.VO.product.ProductCategorySearchVO;
import com.ruoyi.common.entity.VO.product.ProductCategoryVO;
import com.ruoyi.common.entity.VO.product.ProductSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopProductSearchVO;
import com.ruoyi.common.exception.BusinessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements IProductCategoryService {

    @Resource
    private IProductCategoryMapper categoryMapper;

    @Resource
    private IProductService productService;

    @Override
    public List<ProductCategoryVO> searchList(ProductCategorySearchVO productCategorySearchVO) {
        if (productCategorySearchVO.getLevel() == 0) {
            productCategorySearchVO.setParentId((long) 0);
        } else if (productCategorySearchVO.getLevel() == 1) {
            productCategorySearchVO.setLevel(1);
        } else {
            throw new BusinessException("暂不支持查询该层级的分类！");
        }
        return categoryMapper.searchList(productCategorySearchVO);
    }

    @Override
    public List<ProductCategoryVO> searchPetrolTreeList(ShopProductSearchVO searchVO) {
        ProductCategorySearchVO productCategorySearchVO = new ProductCategorySearchVO();
        productCategorySearchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.PETROL.getValue());
        productCategorySearchVO.setIsDelete(0);
        productCategorySearchVO.setIsEnable(1);
        productCategorySearchVO.setShopId(searchVO.getShopId());
        List<ProductCategoryVO> categoryList = categoryMapper.searchList(productCategorySearchVO);
        if(categoryList.isEmpty()){
            return categoryList;
        }
        searchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.PETROL.getValue());
        for (ProductCategoryVO one:categoryList){
            searchVO.setProductCategoryId(one.getId());
            List<PetrolProductVO> petrolList = productService.searchPetrolList(searchVO);
            if(petrolList.size()>0){
                for (PetrolProductVO one2:petrolList){
                    searchVO.setProductId(one2.getProductId());
                    one2.setAttrList(productService.searchAttributeList(searchVO));
                }
            }
            one.setPetrolList(petrolList);
        }
        return categoryList;
    }

//    @Override
//    public String getReviewFilterCategoryIdStr(Long shopId) {
//        return null;
//    }

//    @Override
//    public List<ProductCategoryVO> searchTreeList(Long shopId) {
//        List<ProductCategoryVO> firstList = new ArrayList<>();
//        List<ProductCategoryVO> secondList = new ArrayList<>();
//        List<ProductCategoryVO> thirdList = new ArrayList<>();
//        List<ProductCategoryVO> leftList = new ArrayList<>();
//        List<ProductCategoryVO> categoryList = categoryMapper.searchListForTree(shopId);
//        String categoryIds = getReviewFilterCategoryIdStr(shopId);
//        Long[] categoryIdArr = {};
//        if (StringUtils.isNotEmpty(categoryIds)) {
//            categoryIdArr = Convert.toLongArray(categoryIds);
//        }
//        for (ProductCategoryVO productCategoryVO : categoryList) {
//            if (productCategoryVO.getParentId() == 0) {
//                productCategoryVO.setAncestors(null);
//                if (categoryIdArr.length > 0) {
//                    for (int i = 0; i < categoryIdArr.length; i++) {
//                        if (categoryIdArr[i].equals(productCategoryVO.getId())) {
//                            firstList.add(productCategoryVO);
//                            break;
//                        }
//                    }
//                }else{
//                    firstList.add(productCategoryVO);
//                }
//            } else {
//                productCategoryVO.setCoverUrl(null);
//                leftList.add(productCategoryVO);
//            }
//        }
//        for (ProductCategoryVO productCategoryVO : leftList) {
//            if (!productCategoryVO.getAncestors().contains(",")) {
//                productCategoryVO.setAncestors(null);
//                secondList.add(productCategoryVO);
//            } else {
//                productCategoryVO.setAncestors(null);
//                thirdList.add(productCategoryVO);
//            }
//        }
//        for (ProductCategoryVO productCategoryVO2 : secondList) {
//            List<ProductCategoryVO> tmpList = new ArrayList<>();
//            for (ProductCategoryVO productCategoryVO3 : thirdList) {
//                if (productCategoryVO3.getParentId().equals(productCategoryVO2.getId())) {
//                    tmpList.add(productCategoryVO3);
//                }
//            }
//            productCategoryVO2.setChildList(tmpList);
//        }
//        for (ProductCategoryVO productCategoryVO1 : firstList) {
//            List<ProductCategoryVO> tmpList = new ArrayList<>();
//            for (ProductCategoryVO productCategoryVO2 : secondList) {
//                if (productCategoryVO2.getParentId().equals(productCategoryVO1.getId())) {
//                    tmpList.add(productCategoryVO2);
//                }
//            }
//            productCategoryVO1.setChildList(tmpList);
//        }
//        return firstList;
//    }
}
